package youleche.com.business;

import java.sql.Connection;

import youleche.com.connection.ConnectionFactory;
import youleche.com.factory.DaoFactory;
import youleche.com.idao.ISeller;
import youleche.com.model.MSeller;

/**
 * 商家业务处理
 * @author Administrator
 *
 */
public class BSeller {
	/**
	 * 数据库连接
	 */
	private Connection con = null;
	
	/**
	 * 用户访问接口
	 */
	private ISeller handler = null;

	/**
	 * 构造函数
	 */
	public BSeller() {
		this.handler = DaoFactory.getDSeller();
	}
	
	/**
	 * 添加商家信息
	 * @param seller 商家信息
	 * @return 返回添加影响的行数
	 * @throws Exception 异常
	 */
	public int addSellerInfo(MSeller seller) throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheWriteConnection();
			seller.setA1("0");
			return this.handler.addSellerInfo(this.con, seller);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 根据用户Id获取商家信息
	 * @param userId 用户ID
	 * @return 返回商家信息
	 * @throws Exception 异常
	 */
	public MSeller getSellerByUserId(String userId)
			throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheReadConnection();
			return this.handler.getSellerByUserId(this.con, userId);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 更新商家信息
	 * @param seller 商家信息
	 * @return 返回更新影响的行数
	 * @throws Exception 异常
	 */
	public int updateSellerInfoById(MSeller seller)
			throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheWriteConnection();
			return this.handler.updateSellerInfoById(this.con, seller);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 更新商家认证图片地址
	 * @param seller 商家信息
	 * @return 返回更新影响的行数
	 * @throws Exception 异常
	 */
	public int updateSellerImageUrlById( MSeller seller)
			throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheWriteConnection();
			return this.handler.updateSellerImageUrlById(this.con, seller);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 得到商家的信用指数
	 * @param sellserId 商家ID
	 * @return 返回信用指数
	 * @throws Exception
	 */
	public String getSellerCreditById(String sellserId)
			throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheReadConnection();
			return this.handler.getSellerCreditById(this.con, sellserId);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 更新商家信用指数
	 * @param sellerId 商家Id
	 * @param creditIndex 信用指数
	 * @return 返回更新影响的行数
	 * @throws Exception
	 */
	public int updateSellerCreditById(String sellerId, String creditIndex)
			throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheWriteConnection();
			return this.handler.updateSellerCreditById(this.con, sellerId, creditIndex);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}

}
